<?php


namespace Hilaoyu\LaravelExtend\Services\Files\Redefinition;


use Illuminate\Support\Str;
use League\Flysystem\Config;
use Overtrue\Flysystem\Cos\CosAdapter;


class CosAdapterFix extends CosAdapter
{
    public function __construct(array $config = [])
    {
        $this->config = $config;

        $prefix = data_get($this->config,'root');
        if (!empty($prefix)) {
            $this->setPathPrefix($prefix);
        }
    }

    public function applyPathPrefix($path)
    {
        $path = ltrim($path, '\\/');
        $prefix = $this->getPathPrefix();
        if(!Str::startsWith($path,$prefix)){
            $path = $prefix . $path;
        }
        return $path;
    }

   public function getUrl($path)
   {
        $path = $this->applyPathPrefix($path);

       if('public' != data_get($this->config,'visibility')){
           $temp_url_expire_time = data_get($this->config,'temp_url_expire_time');
           if($temp_url_expire_time <= 0){
               $temp_url_expire_time = 60;
           }
           $expires = '+'.$temp_url_expire_time.' minutes';
           return  $this->getTemporaryUrl($path,$expires);
       }

       return parent::getUrl($path);

   }



    /**
     * @param string $path
     * @param string $contents
     * @param Config $config
     *
     * @return array|false
     */
    public function write($path, $contents, Config $config)
    {
        $path = $this->applyPathPrefix($path);
        return parent::write($path,$contents,$config);
    }

    /**
     * @param string   $path
     * @param resource $resource
     * @param Config   $config
     *
     * @return array|false
     */
    public function writeStream($path, $resource, Config $config)
    {
        $path = $this->applyPathPrefix($path);
        return parent::writeStream($path,$resource,$config);
    }



    /**
     * @param string $path
     * @param string $to
     *
     * @return bool
     */
    public function copy($path, $to)
    {
        $path = $this->applyPathPrefix($path);
        return parent::copy($path,$to);
    }

    /**
     * @param string $path
     *
     * @return bool
     */
    public function delete($path)
    {
        $path = $this->applyPathPrefix($path);
        return parent::delete($path);
    }

    /**
     * @param string $dirname
     *
     * @return bool
     */
    public function deleteDir($dirname)
    {
        $dirname = $this->applyPathPrefix($dirname);
        return parent::deleteDir($dirname);
    }

    /**
     * @param string $dirname
     * @param Config $config
     *
     * @return array|bool
     */
    public function createDir($dirname, Config $config)
    {
        $dirname = $this->applyPathPrefix($dirname);
        return parent::createDir($dirname,$config);
    }

    /**
     * @param string $path
     * @param string $visibility
     *
     * @return array|false
     */
    public function setVisibility($path, $visibility)
    {
        $path = $this->applyPathPrefix($path);
        return parent::setVisibility($path, $visibility);
    }

    /**
     * @param string $path
     *
     * @return bool
     */
    public function has($path)
    {
        $path = $this->applyPathPrefix($path);
        return parent::has($path);
    }

    /**
     * @param string $path
     *
     * @return array|bool
     */
    public function read($path)
    {
        $path = $this->applyPathPrefix($path);
        return parent::read($path);
    }



    /**
     * @param string $path
     *
     * @return array|bool
     */
    public function readStream($path)
    {
        $path = $this->applyPathPrefix($path);
        return parent::readStream($path);
    }


    /**
     * @param string $path
     *
     * @return array|bool
     */
    public function getMetadata($path)
    {
        $path = $this->applyPathPrefix($path);
        return parent::getMetadata($path);
    }


    /**
     * @param string $path
     *
     * @return array|bool
     */
    public function getVisibility($path)
    {
        $path = $this->applyPathPrefix($path);
        return parent::getVisibility($path);
    }


    /**
     * @param string $directory
     * @param bool   $recursive
     *
     * @return mixed
     */
    protected function listObjects($directory = '', $recursive = false)
    {
        $directory = $this->applyPathPrefix($directory);
        return parent::listObjects($directory,$recursive);
    }



}